package net.wsfcp.android.utils;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Build;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.telephony.TelephonyManager;

import net.wsfcp.android.base.BaseApplication;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CommonUtils {

    //string转颜色
    public static int getColor(String str) {

        String baseColor = str;
        if (baseColor.startsWith("0x")) {
            baseColor = baseColor.replaceAll("0x", "#");

        }

        int color = Color.parseColor(baseColor);

        return color;

    }

    //获取手机设备id
    public static String getPhoneId() {
        return Settings.Secure.getString(BaseApplication.getContext().getContentResolver(), Settings.Secure.ANDROID_ID);
    }

    /**
     * 用java代码的方式动态图片选择器
     */
    public static Drawable drawableSelector(Drawable checkpic, Drawable normalpic) {
        StateListDrawable drawable = new StateListDrawable();
        drawable.addState(new int[]{android.R.attr.state_checked}, checkpic);//  状态  , 设置按下的图片
        drawable.addState(new int[]{}, normalpic);//默认状态,默认状态下的图片

        //根据SDK版本设置状态选择器过度动画/渐变选择器/渐变动画
//        if (Build.VERSION.SDK_INT > 10) {
//            drawable.setEnterFadeDuration(500);
//            drawable.setExitFadeDuration(500);
//        }

        return drawable;
    }

    /**
     * 用java代码的方式动态生成颜色选择器
     */
    public static ColorStateList colorSelector(int checkcolor, int normalcolor) {

        int[][] states = new int[][]{
                new int[]{android.R.attr.state_checked}, // checked
                new int[]{-android.R.attr.state_checked}  // unchecked
        };//把两种状态一次性添加

        int[] colors = new int[]{
                checkcolor,
                normalcolor
        };//把两种颜色一次性添加


        ColorStateList colorStateList = new ColorStateList(states, colors);

        return colorStateList;
    }

    //判断邮箱格式
    public static boolean isEmail(String email) {
        if (null == email || "".equals(email)) return false;
        Pattern p = Pattern.compile("\\w+@(\\w+.)+[a-z]{2,3}"); //简单匹配
//        Pattern p = Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");//复杂匹配
        Matcher m = p.matcher(email);
        return m.matches();
    }

}
